// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var validate = require("validate");
	var constant = require("constant");
	var css_announce = require("./platform_main_announcement_manager.css");
	var router = require("../hash/pageRouter");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var oData = {
		content: "",
		postStartDate: "",
		postEndDate: "",
		title: "",
		pageIndex: pageIndex,
		pageSize: pageSize
	};
	var init = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		initEvent();
		getAnnounceInfo(false);
	};
	var initEvent = function() {
		//修改顶部栏的选中状态
		$(".nav_hover", window.parent.document).removeClass("head_link_1");
		$("#company", window.parent.document).addClass("head_link_1");
		// 删除按钮
		$("#main").on("click", ".announce_delete_btn", function() {
			var _this = this;
			dialog.showConfirmDialog("公告删除", "是否确认删除该公告？", function() {
				var bulletinId = $(_this).parents("tr.table-data").attr("bulletinId");
				$(_this).parent().parent().remove();
				announceDelete(bulletinId);
			});
		});
		// 根据条件查找按钮
		$(".middle_set").on("click", ".blue_search_btn", function() {
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
			var cName = base.parseForm($(".middle_set"));
			for (var prop in cName) {
				oData[prop] = cName[prop];
			};
			if (oData.title.replace(/\s+/g, "") == "" && oData.postStartDate.replace(/\s+/g, "") == "" && oData.postEndDate.replace(/\s+/g, "") == "") {
				getAnnounceInfo(false);
			} else {
				getAnnounceInfo(true);
			};
		});

		$("#main").on("click", "#releaseAnnounce", function() {
			router.route("announceRelease/platform_main_announcement_release.html");
		});

		//		$("#main").on("click", ".announce_details_link", function(){
		//			router.route("announceDetail/announceDetail.html?bulletinId="+$(this).parent().attr("id"));
		//		});

	};
	//删除公告弹框
	var deleteItem = function(bulletinId) {
		var deleteItemDialog = $("#delete_item_dialog");
		// 调用dialog中的方法显示dialog，才能用dialog其中的方法关闭与取消dialog
		dialog.showDialog(deleteItemDialog, function() {
			// 确定按钮要做的事
			announceDelete(bulletinId);
			dialog.hideDialog(deleteItemDialog);
		});
	};
	//删除公告接口
	var announceDelete = function(bulletinId) {
		base.fnAjax({
			action: "disableBulletin.json",
			oData: {
				bulletinId: bulletinId
			},
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "删除成功！");
					if ((totalRecordCount - 1) % totalPageCount == 0) {
						oData.pageIndex -= 1;
					};
					getAnnounceInfo(false);
				} else {
					util.alert("删除失败");
				}
			}
		});

	};
	//获取公告内容
	var getAnnounceInfo = function(isSearch) {
		var toTable = function(data) {
			var tpl = [
				'{@each datas as it}',
				'<tr class="table-data" bulletinId="${it.bulletinId}" bulletTitle="${it.title}">',
				'<td class="title_set">${it.title}</td><td>',
				'{@each it.audiences as item}',
				'${getValueByKey(2,item)} ',
				'{@/each}',
				// '<td>${getValueByKey(2,it.audiences)}</td>',
				'</td><td>${it.poster}</td>',
				'<td>${it.postTime}</td>',
				'<td><a id="${it.bulletinId}"><span class="announce_details_link">详情&nbsp;</span></a>',
				'<span class="bar">|</span>',
				'<span class="announce_delete_btn">&nbsp;删除</span></td>',
				'</tr>',
				'{@/each}'
			].join('');
			juicer.register("getValueByKey", constant.getValueByKey);
			$("#announce_table tbody").html("");
			$("#announce_table tbody").html(juicer(tpl, data));
		}
		base.fnAjax({
			action: "inquireBulletinList.json",
			oData: oData,
			nocode: false,
			version: "v1.0",
			fnSuccess: function(data) {
				if (data.status == 200) {
					toTable(data);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					if (isSearch) {
						$(".nofindclass").text("没有符合条件的记录，请尝试其他搜索条件");
						if (data.datas.length == 0) {
							$(".nofindclass").removeClass('none');
						} else {
							$(".nofindclass").addClass('none');
						};
					} else {
						$(".nofindclass").text("没有更多数据了");
						if (data.datas.length == 0) {
							$(".nofindclass").removeClass('none');
						} else {
							$(".nofindclass").addClass('none');
						};
					};
					$(".announce_details_link").click(function() {
						router.route("announceDetail/announceDetail.html?bulletinId=" + $(this).parent().attr("id"));
					});
					base.pageControl($("#announcePagenation"), totalRecordCount, data.pageIndex, data.pageSize, function(p) {
						oData.pageIndex = p;
						getAnnounceInfo(false);
					});
				} else {
					util.alert(data.message);
				}
			}
		});
	}
	return {
		init: init
	};
});